package com.free.space.controller.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *mq客户端测试
 */
@RestController
@RequestMapping("mqClientTest")
public class MqClientTest {
    List<String> list =  new ArrayList<>();
    @GetMapping()
    public String getTextMessage(@RequestParam(value="key",required=false)String key){
        //List<String> resList = new ArrayList<>();
        if(null == list){
            return "[]";
        }
        /*for(String str : list){
             if (null !=key && str.contains(key)){
                 resList.add(str);
             }
        }*/
        if (null !=key ) {
            List<String> list2 = this.list.stream().filter(s -> s.contains(key)).collect(Collectors.toList());
            //return JSON.toJSONString(list2);
            return JSONObject.toJSONString(list2);
        }
        return JSONObject.toJSONString(list);
    }
    @RequestMapping("delete")
    public String delete(){
        list =  new ArrayList<>();
        return "OK";
    }

    /*
     * 监听和接收  队列消息
     */
    @JmsListener(destination="${spring.activemq.queue}")
    public void readActiveQueue(String message) {
        System.out.println("---------------接收到：" + message);
        list.add(message);
    }

}
